/**
 * jims
 */
package com.jims.api.v1.controller.sys;

import com.jims.master.common.data.Message;
import com.jims.master.common.data.StringData;
import com.jims.master.common.utils.SecurityUtil;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.base.user.entity.MasterContactinfo;
import com.jims.master.modules.base.user.service.MasterContactinfoService;
import com.jims.master.modules.patient.myFriend.entity.SysUserFriend;
import com.jims.master.modules.patient.myFriend.service.SysUserFriendService;
import com.jims.master.modules.sys.entity.User;
import com.jims.master.modules.sys.service.SysLoginNameService;
import com.jims.master.modules.sys.service.UserService;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * 建立好友关系Controller
 * @author WangYQ
 * @version 2017-12-23
 */
@Controller
@RequestMapping(value = "/user")
public class SysUserFriendController extends BaseController {

	@Autowired
	private SysUserFriendService sysUserFriendService;

	@Autowired
	private MasterContactinfoService masterContactinfoService;

	@Autowired
	private UserService userService;

	private SysLoginNameService sysLoginNameService;
	@ModelAttribute
	public SysUserFriend get(@RequestParam(required=false) String id) {
		SysUserFriend entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = sysUserFriendService.get(id);
		}
		if (entity == null){
			entity = new SysUserFriend();
		}
		return entity;
	}

	@RequestMapping(value = "deleteFriend")
	@ResponseBody
	public StringData deleteFriend(String faccid) {
	    StringData data=new StringData();
		SysUserFriend userFriend=new SysUserFriend();
		userFriend.setUser(UserUtils.getUser());
		userFriend.setFaccid(faccid);
	    SysUserFriend sysUserFriend=sysUserFriendService.findByFaccid(userFriend);
		int i=sysUserFriendService.delete(sysUserFriend);
        if(i>0){
            data.setCode("success");
            data.setData("删除好友关系成功");
        }else{
            data.setCode("error");
            data.setData("删除好友关系失败");
        }
		return data;
	}
	/**
	 * 通过名字查找好友
	 * @param  mobilePhone   电话号码
	 * @return user
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "findUser")
	@ResponseBody
	public User findUser(String mobilePhone){
		StringData data=new StringData();
		MasterContactinfo masterContactinfo= masterContactinfoService.findByMobliePhone(mobilePhone);
		User user=new User();
		if(masterContactinfo !=null){
			user=userService.findUserByPersonId(masterContactinfo.getBaseinfo().getId());
		}
		return user;
	}
	/**
	 * @Title: V100addFriend
	 * @Description: （添加好友）
	 * @param userId 网易账号(用户id)
	 * @param fid 要添加人的账号
	 * @return java.lang.String
	 * @author WangYQ
	 * @date 2017-12-23
	 * @throws
	 */
	@RequestMapping(value = "V100/addFriend",method ={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public String addFriend(@RequestParam(required=true) String userId,String fid){
		Message msg=new Message();
		User user= userService.findUserByAccount(userId);
		SysUserFriend sysUserFriend=new SysUserFriend();
		sysUserFriend.setUser(user);
		sysUserFriend.setFriendId(fid);
		sysUserFriend.setFaccid(fid.substring(0,32));
		int i=sysUserFriendService.save(sysUserFriend);
		if(i>0){
			msg.setData("添加好友成功");
		}else{
			msg.setStatus("0");
			msg.setErrCode("414");
			msg.setErrMsg("添加好友失败，请联系管理人员");
		}
		return SecurityUtil.encryptMsgData(msg);
	}
}